package net.loodies;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.net.Uri;
import android.view.MotionEvent;
import android.widget.ImageView;


public class LoodiesFixedView extends ImageView implements
LoodiesFixedViewLoaderCallback {

	Activity _Activity;
	String loodieType;
	String slotID;
	String userID;
	Bitmap bitmap = null;
	RectF fixedRectangle = new RectF(0, 0, 320, 50);
	String jString;
	LoodiesFixedViewLoader loader;

	public LoodiesFixedView(Activity _Activity, String _slotID, String _userID) {
		super(_Activity);
		this._Activity = _Activity;
		slotID = _slotID;
		userID = _userID;

		String strURL = "http://thx.swelen.com/mobileServe/"
			+ "?response=mixed&slot_uid=" + slotID + "&client_uid="
			+ userID;
		bitmap = Bitmap.createBitmap(320, 50, Bitmap.Config.ARGB_8888);
		setImageBitmap(bitmap);
		setFocusable(true);
		loader = new LoodiesFixedViewLoader(strURL, this, this, _Activity);
	}

	public void LoodiesFixedViewLoaded() {

		
	}

	public void SetRect(RectF _rect) {
		fixedRectangle = _rect;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

	//	int ex = (int) event.getX();
	//	int ey = (int) event.getY();

		if (event.getAction() == MotionEvent.ACTION_DOWN) {

			if (LoodiesParameters.map != null) {
				String notify = LoodiesParameters.map
				.get("click_notification_url");

				URL myFileUrl = null;
				try {
					myFileUrl = new URL(notify);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				HttpURLConnection conn = null;
				try {
					conn = (HttpURLConnection) myFileUrl.openConnection();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				try {
					conn.connect();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				@SuppressWarnings("unused")
				int length = conn.getContentLength();
				try {
					@SuppressWarnings("unused")
					InputStream is = conn.getInputStream();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String destination = LoodiesParameters.map
				.get("ad_destination_url");

				if (AccelerometerManager.isListening()) {
					AccelerometerManager.stopListening();
				}
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(destination));
				_Activity.startActivity(i);

			}

		}

		return true;

	}

}
